package app.dao;


import app.entity.User;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.util.List;

public interface UserDao {
    @SqlUpdate("INSERT INTO user (name, age, mobile, remark) VALUES (:name, :age, :mobile, :remark)")
    @GetGeneratedKeys
    Long insert(@BindBean User user);

    @SqlUpdate("UPDATE user SET name=:name, age=:age, mobile=:mobile, remark=:remark WHERE id=:id")
    int update(@BindBean User user);

    @SqlQuery("SELECT * FROM user WHERE id = :id")
    @RegisterBeanMapper(User.class)
    User findById(@Bind("id") Long id);

    @SqlUpdate("DELETE FROM user WHERE id = :id")
    int deleteById(@Bind("id") Long id);

    @SqlQuery("SELECT * FROM user LIMIT :limit OFFSET :offset")
    @RegisterBeanMapper(User.class)
    List<User> findAll(@Bind("limit") int limit, @Bind("offset") int offset);

    @SqlQuery("SELECT COUNT(*) FROM user")
    int totalCount();
}